package com.finance.cooperate.feature.core.service.impl;

import com.finance.cooperate.dao.repository.secondary.ScrapyAppRepository;
import com.finance.cooperate.dao.support.dto.ScrapyAppCategoriesDTO;
import com.finance.cooperate.feature.core.service.ScrapyAppsCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ScrapyAppsCacheServiceImpl implements ScrapyAppsCacheService {

    public static final String CACHE_NAME = "scrapy";

    @Autowired
    private ScrapyAppRepository scrapyAppRepository;

    @Cacheable(value = CACHE_NAME, key = "'key_'+#categoriesId", unless = "#result==null")
    @Override
    public List<ScrapyAppCategoriesDTO> getByCategories(String categoriesId) {

        List<ScrapyAppCategoriesDTO> result = scrapyAppRepository.findByCategories(categoriesId);

        return result;
    }

    @Cacheable(value = CACHE_NAME, key = "'key_'+#categoriesId+'_'+#count", unless = "#result==null")
    @Override
    public List<ScrapyAppCategoriesDTO> getByCategories(String categoriesId, int count) {

        List<ScrapyAppCategoriesDTO> result = scrapyAppRepository.findByCategories(categoriesId, count);

        return result;
    }

}
